package com.daJiao.domain;

import com.daJiao.entity.SysUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class SecurityUserDetails implements UserDetails {

	private static final long serialVersionUID = 5800599093391505463L;

	private Long userId;

	private String username;

	private String realName;

	private String password;

	private String email;

	private String phone;

	private List<String> authorityList;

	public SecurityUserDetails(SysUser user, List<String> authorities) {
		if (user != null) {
			this.setUserId(user.getId());
			this.setUsername(user.getUserName());
			this.setPassword(user.getPassword());
			this.setRealName(user.getRealName());
			this.setEmail(user.getEmail());
			this.setPhone(user.getMobile());
			this.authorityList = authorities;
		}
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> grantedAuthorities =
				authorityList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
		return grantedAuthorities;
	}

	@Override
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	@Override
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<String> getAuthorityList() {
		return authorityList;
	}

	public void setAuthorityList(List<String> authorityList) {
		this.authorityList = authorityList;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}


	@Override
	public boolean isAccountNonLocked() {
		return true;
	}


	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}


	@Override
	public boolean isEnabled() {
		return true;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

}
